package com.example.practice.fragment;

import java.util.ArrayList;
import java.util.List;



import com.example.practice.BaseActivity;
import com.example.practice.MainActivity;
import com.example.practice.R;
import com.example.practice.adapter.SensorGridAdapter;
import com.example.practice.bean.SensorBean;
import com.example.practice.bean.SensorValue;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class RealTimeFragment extends Fragment{

	private GridView gridView;
	private SensorGridAdapter sensorGridAdapter;
	private List<SensorBean> sensorBeans = new ArrayList<SensorBean>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_realtime, null);
		gridView = (GridView) view.findViewById(R.id.grid_view);
		sensorGridAdapter = new SensorGridAdapter(sensorBeans);
		gridView.setAdapter(sensorGridAdapter);
		
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				MainActivity mainActivity = (MainActivity) getActivity();
				mainActivity.getSupportFragmentManager().beginTransaction()
				.addToBackStack(null)
				.replace(R.id.realtabcontent, mainActivity.realTimeChartFragment)
				.commit();

//				startActivity(new Intent(getActivity(),ChartActivity.class));
			}
		});
		return view;
	}
	
	public void  onGetSensorValue(SensorValue sensorValue) {
		SensorBean sensorCO2 = new SensorBean();
		SensorBean sensorLight = new SensorBean();
		SensorBean sensorAirHumi = new SensorBean();
		SensorBean sensorAirTemp = new SensorBean();
		SensorBean sensorSoildTemp = new SensorBean();
		SensorBean sensorSoildHumi = new SensorBean();

		sensorSoildHumi.setName("土壤湿度");
		sensorSoildHumi.setValue(sensorValue.getSoilHumid());
		sensorSoildTemp.setName("土壤温度");
		sensorSoildTemp.setValue(sensorValue.getSoilTemper());
		sensorAirTemp.setName("空气温度");
		sensorAirTemp.setValue(sensorValue.getAirTemper());
		sensorAirHumi.setName("空气湿度");
		sensorAirHumi.setValue(sensorValue.getAirHumid());
		sensorCO2.setName("CO2");
		sensorCO2.setValue(sensorValue.getCo2());
		sensorLight.setName("光照强度");
		sensorLight.setValue(sensorValue.getLight());

		sensorBeans.clear();
		sensorBeans.add(sensorCO2);
		sensorBeans.add(sensorAirHumi);
		sensorBeans.add(sensorAirTemp);
		sensorBeans.add(sensorLight);
		sensorBeans.add(sensorSoildHumi);
		sensorBeans.add(sensorSoildTemp);

		sensorGridAdapter.notifyDataSetChanged();
	}
	
	
}
